CONTENTS | INDEX | PREV | NEXT

                          Crap Calc


A Step in the Right Direction, by Matthew J Fletcher, 23/10/1998

Now lets try something just a little bit harder. For this, I will presume a
few things. Firstly, you have been to school for at least a few years and are
aware that mathematics exists, and secondly, have a keen desire to make the
computer do something that involves you.

Unlike Matts first tutorial, I will also presume that you are capable of
using a text editor, yes we are onto advanced stuff now.

Here goes then.....

- (1) Planning
Yes, yes. Planning is important and it is good to get you into it at this
very early stage, because when you do this at university, you will spend most
of your time being wined at for being a hacker (in your style of coding) i.e.
you make it up as you go along and fix any problems without checking the
plan of what the program is supposed to be doing.

- A simple ANSI calculator, (i.e. no Amiga or PC, Windows, etc)
- Just simple integer (whole number maths)
- One function only - add (err, the schools still teach this I presume)

So if we right it out in rough English first so we know what we will be
going on...., remember this is not C.


Store Number_1  <-- so if we are going to add two numbers we need to stores
Store Number_2      or variables, and a third for the output from the magic
Store Result        that will go on inside. 

Output "Hello I am Crap Calc"  <-- We are just telling the world we exist

Output "Please enter, your first number" <-- Asking the user first input
Input "Number_1"  <-- we store there choice in here

Output "Please enter, your second number" <-- Asking the user second input
Input "Number-2"  <-- again we store the choice

Number_1 + Number_2 = Result <-- now dot get to excited, I am sure you have
                                 done this before, we are just adding the
                                 numbers that were chosen and then recording
                                 the result in a fresh space.

Output "The Result was", Result <-- all we did there was issue a message and
                                    output was we had worked out together.


Thats it, very tricky i will admit now all we have to do is change it into C

- (2) Creating the program
Before we go any further, you totally understand the last section don't you?
If not, read it again!
We will now create a working C version from the program above.

You can now get you reference book out and check I am doing this properly or
just type it in and hope. Bear mind that you only have to type the stuff in
bold text right....(this is important).


----------- Start of Program (no not this line its not bold, got it) -------

//ough by the way the two forward slashes are a way of putting comments in
//so that C wont mind us using English....

  //Program Crap Clac
  //By The Readers name
  //On the date read

  #include <stdio.h>
  #include <stdlib.h>

These are vital elements of the C language that make to program work, even
one this simple, if you have used the correct assigns then no more path is
required to compile properly.

  int Number_1;  this sets up the first store as an integer
  int Number_2;  the same again, note that the store names must be kept
  int Result;    the same whenever they are used in the program

  main()   main() is the well, main point in the program, more correctly
                  it is the starting point, be that not important now.
  {      this bracket starts the section that will be run

  printf("Welcome to Crap Calc\n");
  printf("\n");
  
This just puts a space in to make it look neat. You should be quite familiar
with printf by now shouldn't you.

  printf("read to do some adding ? \n");
  printf("\n");
  
You may well be thinking what what the hell are all these \n things well
they just tell c to go onto a \(n)ew line, at the end of this one.

  printf("Please enter your first number...\n");
  scanf("%d",&Number_1); 
  
Now keep calm, this is just how you read a input for the user in C.
%d stands for a decimal storage box, and & is the pointer to the memory
address, rather than the real address, If you didn't understand that, its
OK. I didn't at first either, but hey, you don't have to. Just type it in for now.
                             
  printf("Please enter your second number...\n");
  scanf("%d",&Number_2);

See we are following the plan and have just told the program to ask the user
for the next number to input.

  printf("\n");
  Result = Number_1 + Number_2;

Right, this is just the calculation bit. We are adding two numbers from two
locations to make one in one new location, and yes + is the same in C as in
English. You might be wondering why the result is at the wrong end of the
sum? Well C has to have the box ready (Result) to put the result in you see.

  printf("The total is:= %d \n",Result);
  return(0);

This just outputs the result (Result), in a %d (decimal) format, along in
a line with some text, the return(0) line can be totally ignored at this
stage, as it quite literally does nothing at this level.

  }

--------------- End of Program (no not this line, right...) -------------


- (3) Compilation Time 

- You should be able to do this by your self but go on, save out the file
to ram:crapcalc.c then open the shell (Get a new one, KingCON, CornShell,
VincED or any scrolling shell is a must), get to the abin directory mine
is work:dice/abin/ type dcc ram:crapclac.c -o ram:crapclac and the
program should appear after some grinding by dice. WOW! Click on it and
play. Now isn't that a good feeling - your first program that does something.


- (4) Why doesn't it work ?
It does. You have made a mistake or the laws of physics have changed. Its
C Jim but not as DICE knows it..... kind of. Here are some of things you
should check..

- Capitals. C is CASE SENSITIVE. If it has capitals in it, leave it that way,
if it does not, then don't change it either - just check your version is the
same as mine.

- Once again, check that it is exactly the same -  both { } brackets should
exist. If you are quite bright, check the line number that DC1 (I presume)
reports that the error is on, and then check that line in your code TWICE.

- If it cant find the file, check it has a .c on the end, and the path is
correct, bung it in ram: to be sure.

- If you get errors from DCPP, or DAS then you have either gone totally mad,
have got your text editor saving out rich text for ANSI or even some custom
format, these will not work. ASCII text is the way to go.


- (5) More Advanced Stuff

- How about changing int in the vars to float i.e. integer to floating point
so you can use decimal point numbers properly and then try adding numbers
like 3.1 or even 0.1! wow...

- How would you change this to do subtraction? hmm...think about it. Only one
thing needs changing... back to the maths room..... when you have done this
devision and multiplying are but one more step away. Just think, windows
programers can sell stuff like this for money...., cool.

- Try adding more vars, so you can add or devide loads of numbers at once.

- What else? Well not much. Try the next tutorial and get a book on C so
you can do some bedtime reading and maybe hack the examples to get something
more interesting to happen.